package com.jhd.fuzhou.view.task;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.util.StringShow;
import com.jhd.fuzhou.vo.QuestionBankOptionVo;
import com.jhd.fuzhou.vo.SupervisionInspectionContentVo;
import com.jhd.fuzhou.vo.SupervisionInspectionVo;

import java.util.List;

import butterknife.BindView;

/*检查内容*/
public class InspectResultActivity extends BaseActivity {

    private SupervisionInspectionVo supervisionInspectionVo;
    private Context context=this;
    @BindView(R.id.rv_check_result)RecyclerView rv_check_result;
    @BindView(R.id.tv_check_record)TextView tv_check_record;//检查记录
    @BindView(R.id.tv_supervision)TextView tv_supervision;//监督意见

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_inspect_result);
    }

    @Override
    protected void processLogic() {
        onBack();
        String data=getIntent().getStringExtra("data");
        supervisionInspectionVo= JSON.parseObject(data, SupervisionInspectionVo.class);
        tv_check_record.setText(StringShow.showStr(supervisionInspectionVo.getInspectRecords()));
        tv_supervision.setText(StringShow.showStr(supervisionInspectionVo.getSupervisionOpinions()));
        List<SupervisionInspectionContentVo> specialSuperviseInspectionContentVoList = supervisionInspectionVo.getSpecialSuperviseInspectionContentVoList();
        List<SupervisionInspectionContentVo> supervisionInspectionContentList = supervisionInspectionVo.getSupervisionInspectionContentList();
        if(specialSuperviseInspectionContentVoList!=null){//专项监督
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            rv_check_result.setLayoutManager(layoutManager);
            QuestionTreeAdapter adapter=new QuestionTreeAdapter(specialSuperviseInspectionContentVoList,1);
            rv_check_result.setAdapter(adapter);
        }else if(supervisionInspectionContentList!=null){//双随机，日常监督
            LinearLayoutManager layoutManager = new LinearLayoutManager(context);
            rv_check_result.setLayoutManager(layoutManager);
            QuestionTreeAdapter adapter=new QuestionTreeAdapter(supervisionInspectionContentList,2);
            rv_check_result.setAdapter(adapter);
        }
    }
    public class QuestionTreeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private List<SupervisionInspectionContentVo> supervisionInspectionContentList;private int type;

        public QuestionTreeAdapter(List<SupervisionInspectionContentVo> supervisionInspectionContentList,int type) {
            this.supervisionInspectionContentList = supervisionInspectionContentList;
            this.type = type;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            RecyclerView.ViewHolder viewHolder = getViewHolderByViewType(viewType);
            return viewHolder;
        }

        private RecyclerView.ViewHolder getViewHolderByViewType(int viewType) {
            View itemTitle = View.inflate(context,R.layout.item_question2, null);
            View itemView = View.inflate(context,R.layout.item_question_tree_4, null);
            RecyclerView.ViewHolder holder=null;
            switch (viewType){
                case 0:
                    holder=new QuestionTreeAdapter.ViewHolderTitle(itemTitle);
                    break;
                case 1:
                    holder=new QuestionTreeAdapter.ViewHolderItem(itemView);
                    break;
            }
            return holder;
        }

        @Override
        public int getItemViewType(int position) {
            if(supervisionInspectionContentList.get(position).getQuestionBankOptionVoList()==null){
                return 0;
            }else{
                return 1;
            }
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if(getItemViewType(position)==0){
                if(type==1){
                    ((QuestionTreeAdapter.ViewHolderTitle)holder).tv_question.setText(supervisionInspectionContentList.get(position).getQuestionName());
                }else{
                    ((QuestionTreeAdapter.ViewHolderTitle)holder).tv_question.setText(supervisionInspectionContentList.get(position).getQuestionBankName());
                }
            }
            if(getItemViewType(position)==1){
                if(type==1){
                    ((QuestionTreeAdapter.ViewHolderItem)holder).tv_question.setText(supervisionInspectionContentList.get(position).getQuestionName());
                }else{
                    ((QuestionTreeAdapter.ViewHolderItem)holder).tv_question.setText(supervisionInspectionContentList.get(position).getQuestionBankName());
                }
                List<QuestionBankOptionVo> questionBankOptionVoList = supervisionInspectionContentList.get(position).getQuestionBankOptionVoList();
                for (QuestionBankOptionVo questionBankOptionVo : questionBankOptionVoList) {
                    if(questionBankOptionVo.getId().equals(supervisionInspectionContentList.get(position).getQuestionBankOptionId())){
                        ((QuestionTreeAdapter.ViewHolderItem)holder).tv_answer.setText(questionBankOptionVo.getOptionName());
                        break;
                    }
                }
            }
        }

        @Override
        public int getItemCount() {
            return supervisionInspectionContentList.size();
        }

        class ViewHolderTitle extends RecyclerView.ViewHolder {
            TextView tv_question;
            public ViewHolderTitle(View itemView) {
                super(itemView);
                tv_question = (TextView) itemView.findViewById(R.id.tv_question);
            }
        }
        class ViewHolderItem extends RecyclerView.ViewHolder {
            TextView tv_question,tv_answer;
            public ViewHolderItem(View itemView) {
                super(itemView);
                tv_question = (TextView) itemView.findViewById(R.id.tv_question);
                tv_answer = (TextView) itemView.findViewById(R.id.tv_answer);
            }
        }
    }
}